package com.hyf.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.hyf.commonutils.fruit;
import com.hyf.service.config.exception.GuliException;
import com.hyf.vod.service.VodService;
import com.hyf.vod.utils.ConstanVodutils;
import com.hyf.vod.utils.InitVodClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@RestController
@RequestMapping("eduvod/video")
//@CrossOrigin // 解决跨域问题
@Api(description="阿里云视频点播微服务")
public class VodController {

    @Autowired
    private VodService vodService;

    // 上传视频到阿里云的方法
    @ApiOperation(value = "上传视频方法")
    @PostMapping("uploadAlyVideo")
    public fruit uploadAlyVideo(
        @ApiParam(name = "file", value = "文件", required = true)
         @RequestParam("file") MultipartFile file){
       // 获取上传文件的对象
        String videoId=vodService.uploadVideoAly(file);
        return fruit.ok().message("视频上传成功").data("videoId",videoId);
    }


// 根据视频id 删除阿里云 的视频
@ApiOperation(value = "删除视频的方法")
    @DeleteMapping("removeAlyVideo/{id}")
    public fruit removeAlyVideo(
            @ApiParam(name = "id", value = "云端视频id", required = true)
            @PathVariable String id){
        try {

            // 初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstanVodutils.ACCESS_KEY_ID, ConstanVodutils.ACCESS_KEY_SECRET);
            // 创建删除视频的request 对象
            DeleteVideoRequest request=new DeleteVideoRequest();
            // 向 request 对象里面设置 视频 id
            request.setVideoIds(id);
            // 调用初始化对象的方法 实现删除
            DeleteVideoResponse acsResponse = client.getAcsResponse(request);

            System.out.print("RequestId = " + acsResponse.getRequestId() + "\n");
            // 删除成功返回一个ok
            return fruit.ok();
        }catch (Exception e) {
             e.printStackTrace();
             throw new GuliException(20001,"视频删除失败");
        }

    }





    // 删除多个视频的方法
    // 参数是多个视频id
    @ApiOperation(value = "删除阿里多个视频的方法")
    @DeleteMapping("delete-batch")
    public fruit deleteBatch(
            @ApiParam(name = "videoIdList", value = "云端视频id", required = true)
            @RequestParam("videoIdList") List<String> videoIdList){

        vodService.removeMoreAlyVideo(videoIdList);



        return fruit.ok();
    }






    /**
     * 批量删除视频
     * @param  课件中的方法
     * @return
     */
//    @DeleteMapping("delete-batch")
//    public fruit removeVideoList(
//@ApiParam(name = "videoIdList", value = "云端视频id", required = true)
//        @RequestParam("videoIdList") List videoIdList){
//        vodService.removeVideoList(videoIdList);
//        return fruit.ok().message("视频删除成功");
//    }




//    // 课件中的方法
//    @DeleteMapping("{videoId}")
//    public fruit removeVideo(@ApiParam(name = "videoId", value = "云端视频id", required = true)
//        @PathVariable String videoId){
//        vodService.removeVideo(videoId);
//        return fruit.ok().message("视频删除成功");
//    }

    // 根据视频id 查询视频信息
    @ApiOperation(value = "根据视频id 查询视频信息")
    @GetMapping("get-play-auth/{videoId}")
    public fruit getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {
       
        //获取阿里云存储相关常量
        
        String accessKeyId = ConstanVodutils.ACCESS_KEY_ID;
        String accessKeySecret = ConstanVodutils.ACCESS_KEY_SECRET;
       
        //初始化
        DefaultAcsClient client = InitVodClient.initVodClient(accessKeyId, accessKeySecret);
        
        //请求 把视频id窜进去
        
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        
        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        
        //得到播放凭证
        String playAuth = response.getPlayAuth();
       
        //返回结果
        
        return fruit.ok().message("获取凭证成功").data("playAuth", playAuth);
     
    }
    
}
